home *** CD-ROM | disk | FTP | other *** search
/ HamCall (April 1991) / HAMCALL CD-ROM (Buckmaster)(April 1991).BIN / prgming / ctutor / chap01.txt < prev    next >
Text File  |  1990-10-14  |  8KB  |  232 lines

  1.  
  2.                                                      Chapter 1
  3.  
  4.                                                GETTING STARTED
  5.  
  6.  
  7.  
  8. WHAT IS AN IDENTIFIER
  9. ______________________________________________________________
  10.  
  11. Before you can do anything in any language, you must at least
  12. know how to name an identifier.  An identifier is used for any
  13. variable, function, data definition, etc.  In the programming
  14. language C, an identifier is a combination of alphanumeric
  15. characters, the first being a letter of the alphabet or an
  16. underline, and the remaining being any letter of the alphabet,
  17. any numeric digit, or the underline.  In the case of some
  18. compilers, a dollar sign is permitted but not as the first
  19. character of an identifier.  It should be pointed out that
  20. even though a dollar sign may be permitted by your C compiler,
  21. it is not used anywhere in this tutorial since it is not in
  22. general use by C programmers, and is not even allowed by most
  23. compilers.  If you do not plan to write any portable code, you
  24. can use it at will if you feel it makes your code more
  25. readable.
  26.  
  27.  
  28. Two rules must be kept in mind when naming identifiers.
  29.  
  30. 1.   The case of alphabetic characters is significant.  Using
  31.      "INDEX" for a variable is not the same as using "index"
  32.      and neither of them is the same as using "InDeX" for a
  33.      variable.  All three refer to different variables.
  34.  
  35. 2.   As C is defined, up to 32 significant characters can be
  36.      used and will be considered significant by most
  37.      compilers.  If more than 32 are used, they will be
  38.      ignored by the compiler.
  39.  
  40.  
  41.  
  42. WHAT ABOUT THE UNDERLINE?
  43. ______________________________________________________________
  44.  
  45. Even though the underline can be used as part of a variable
  46. name, and adds greatly to the readability of the resulting
  47. code, it seems to be used very little by experienced C
  48. programmers.  It adds greatly to the readability of a program
  49. to use descriptive names for variables and it would be to your
  50. advantage to do so.  Pascal programmers tend to use long
  51. descriptive names, but most C programmers tend to use short
  52. cryptic names.  Most of the example programs in this tutorial
  53. use very short names for that reason.
  54.  
  55.  
  56.  
  57.  
  58.                                                            1-1
  59.  
  60.                                    Chapter 1 - Getting Started
  61.  
  62. KEYWORDS
  63. ______________________________________________________________
  64.  
  65. There are 32 words defined as keywords in C.  These have
  66. predefined uses and cannot be used for any other purpose in
  67. a C program.  They are used by the compiler as an aid to
  68. compiling the program.  They are always written in lower case.
  69. A complete list follows;
  70.  
  71.       auto        double      int          struct
  72.       break       else        long         switch
  73.       case        enum        register     typedef
  74.       char        extern      return       union
  75.       const       float       short        unsigned
  76.       continue    for         signed       void
  77.       default     goto        sizeof       volatile
  78.       do          if          static       while
  79.  
  80. In addition to this list of keywords, your compiler may use
  81. a few more.  If it does, they will be listed in the
  82. documentation that came with your compiler.  Each of the above
  83. keywords will be illustrated and used in this tutorial.
  84.  
  85.  
  86. WE NEED DATA AND A PROGRAM
  87. ______________________________________________________________
  88.  
  89. Any computer program has two entities to consider, the data,
  90. and the program.  They are highly dependent on one another and
  91. careful planning of both will lead to a well planned and well
  92. written program.  Unfortunately, it is not possible to study
  93. either completely without a good working knowledge of the
  94. other.  For this reason, this tutorial will jump back and
  95. forth between teaching methods of program writing and methods
  96. of data definition.   Simply follow along and you will have
  97. a good understanding of both.  Keep in mind that, even though
  98. it seems expedient to sometimes jump right into the program
  99. coding, time spent planning the data structures will be well
  100. spent and the final program will reflect the original
  101. planning.
  102.  
  103.  
  104. HOW THIS TUTORIAL IS WRITTEN
  105. ______________________________________________________________
  106.  
  107. As you go through the example programs, you will find that
  108. every program is complete.  There are no program fragments
  109. that could be confusing.  This allows you to see every
  110. requirement that is needed to use any of the features of C as
  111. they are presented.  Some tutorials I have seen give very few,
  112. and very complex examples.  They really serve more to confuse
  113. the student.  This tutorial is the complete opposite because
  114. it strives to cover each new aspect of programming in as
  115. simple a context as possible.  This method, however, leads to
  116.  
  117.                                                            1-2
  118.  
  119.                                    Chapter 1 - Getting Started
  120.  
  121. a lack of knowledge in how the various parts are combined.
  122. For that reason, the last chapter is devoted entirely to using
  123. the features taught in the earlier chapters.  It will
  124. illustrate how to put the various features together to create
  125. a usable program.  They are given for your study, and are not
  126. completely explained.  Enough details of their operation are
  127. given to allow you to understand how they work after you have
  128. completed all of the previous lessons.
  129.  
  130.  
  131. RESULT OF EXECUTION
  132. ______________________________________________________________
  133.  
  134. The result of executing each program will be given in comments
  135. at the end of the program listing, after the comment is
  136. defined in about the fourth program of chapter 2.  If you feel
  137. confident that you completely understand the program, you can
  138. simply refer to the result of execution to see if you
  139. understand the result.  In this case, it will not be necessary
  140. for you to compile and execute every program.  It would be a
  141. good exercise for you to compile and execute some of them
  142. however, because all C compilers will not generate exactly the
  143. same results and you need to get familiar with your own
  144. compiler.
  145.  
  146. At this point, you should load and run           =============
  147. FIRSTEX.C if you have not yet done so, to see      FIRSTEX.C
  148. that your C compiler is properly loaded and      =============
  149. operating.
  150.  
  151.  
  152. A DISCUSSION OF SOME OF THE FILES
  153. ______________________________________________________________
  154.  
  155. LIST.EXE
  156.  
  157. This file will list the source files for you with line numbers
  158. and filename.  To use it, simply type "LIST" followed by the
  159. appropriate filename.  Type LIST FIRSTEX.C now for an example.
  160. C source code is given in Chapter 14 for a similar listing
  161. program along with a brief description of its operation.
  162. After you have completed your study of C, you will have the
  163. ability to read and understand the source code for this
  164. program.
  165.  
  166. PRINTALL.BAT
  167.  
  168. This is a batch file that will call the above LIST.EXE file
  169. once for each of the example C programs, printing all of the
  170. files out.  If you want a hardcopy of all of the files, enter
  171. PRINTALL and watch as your printer fills about 70 sheets of
  172. paper with C programs.
  173.  
  174.  
  175.                                                            1-3
  176.  
  177.                                    Chapter 1 - Getting Started
  178.  
  179. THE \ANSWER DIRECTORY
  180. ______________________________________________________________
  181.  
  182. There is a directory on the distribution disk named ANSWER
  183. which contains an answer to each of the programming exercises
  184. given at the end of the chapters.  You should attempt to do
  185. original work on each of the exercises before referring to
  186. these answers, in order to gain your own programming
  187. experience.  These answers are given for your information in
  188. case you are completely stuck on how to solve a particular
  189. problem.  These answers are not meant to be the only answer,
  190. since there are many ways to program anything, but they are
  191. meant to illustrate one way to solve the suggested programming
  192. problem.
  193.  
  194. The answers are all in executable files named in the format
  195. CHnn_m.C where nn is the chapter number, and m is the exercise
  196. number.  If there is more than one answer required, an A, B,
  197. or C is included following the exercise number.
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.                                                            1-4
  231.  
  232.